class VastApi
  class Categories
    module Category
      # Convert a selected node to a Hash. It accepts a CSS3 Selector as an attribute.
      # Returns the hash.
      def categories
        if self['id']
          hash = {}
          self.element_children.each do |node|
            node.extend(Category)
            hash[node['id']] = {
              :name => node['name'],
              :count => node['count']
            }
            if self.element_children.length > 0
              cats = node.categories
              hash[node['id']][:children] = cats unless cats.empty?
            end
          end
          # Return the hash
          hash
        end
      end
    end
  end
end
